package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2019/8/11.
 */
@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper contentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);

    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public Long saveOrUpdate(Travel travel) {

        // 作者id
        // 最后更新时间
        travel.setLastUpdateTime(new Date());

        TravelContent travelContent = travel.getTravelContent();
        // 摘要
        String content = travelContent.getContent();
        if (content.length() >= 200) {
            travel.setSummary(content.substring(0, 200));
        } else {
            travel.setSummary(content);
        }

        Long id = travel.getId();
        if (travel.getId() == null) {
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            id = travel.getId();
            travelContent.setId(id);
            contentMapper.insert(travelContent);
        } else {
            travelMapper.updateByPrimaryKey(travel);
            travelContent.setId(travel.getId());
            contentMapper.updateByPrimaryKey(travelContent);
        }
        return id;
    }

    @Override
    public List<Travel> getViewNumTop3(Long id) {
        return travelMapper.seleceViewNumTop3(id);
    }

    @Override
    public void updateState(Travel travel) {
        if (travel.getState() == 2) {
            travel.setReleaseTime(new Date());
        }
        travelMapper.updateState(travel);
    }

    @Override
    public TravelContent selecteContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getTravelByState(int state) {
        return travelMapper.selectTravelByState(state);
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    public List<Travel> listByUserId(Long uid) {
        List<Travel> travels = travelMapper.selectByUserId(uid);
        for (Travel travel : travels) {
            TravelContent content = contentMapper.selectByUseId(uid);
            travel.setTravelContent(content);
        }
        return travels;
    }

    public List<Travel> selectTravelByUserId(Long id) {
        List<Travel> travels = travelMapper.selectTravelByUserId(id);
        for (Travel travel : travels) {
            TravelContent content = contentMapper.selectTravelByUserId(id);
            travel.setTravelContent(content);
        }
        return travels;
    }

    public void deleteTravel(Long id) {
        travelMapper.deleteByPrimaryKey(id);
        contentMapper.deleteByPrimaryKey(id);
        travelMapper.deleteRelation(id);
    }


}
